//
//  HJStatusPictureView.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/27.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit

import SDWebImage
class HJStatusPictureView: UICollectionView {
    
    //MARK: 属性
    
    private let Cell = "Cell"
    ///显示图片的URL数组
    
    var picturesURL: [NSURL]? {
        
        didSet{
            
            let size = ViewSize()
            
            
            ///自己来更新自己的约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
                ///
                self.reloadData()
                
                
            }
        }
        
    }
    
    
    
    private func ViewSize() -> CGSize{
        
        layout.itemSize = CGSize(width: HJPictureViewCellWH, height: HJPictureViewCellWH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        let count = picturesURL?.count ?? 0
        
        if count == 0 {
            
            layout.itemSize = CGSizeZero
            
            return layout.itemSize
        }
        
        ///一张图
        
        if count == 1{
            
            ///一张图片默认大小
            var imageSize = CGSize(width: HJPictureViewCellWH, height: HJPictureViewCellWH)
            
            ///获取图片URL
            
            let stringURL = picturesURL![0].absoluteString
            
            ///去本地获取对应图片的大小
            
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(stringURL){
                
                
                imageSize = image.size
                
            }
            if imageSize.width < 40{
                imageSize.width = 40
            }
            if imageSize.width > UIScreen.width * 0.5{
                
                imageSize.width = UIScreen.width * 0.5
            }
            
            
            layout.itemSize = imageSize
            
            return imageSize        }
        
        ///多张图片
        layout.minimumInteritemSpacing = HJPictureViewMargin
        layout.minimumLineSpacing = HJPictureViewMargin
        
        var column = HJPictureViewMaxColumn
        
        if count == 2 || count == 4 {
            
            column = 2
        }
        
        let row = (column + count - 1) / column
        
        ///计算宽度
        let width = HJPictureViewCellWH * CGFloat(column) + HJPictureViewMargin * CGFloat(column - 1)
        
        ///计算高度
        let height = HJPictureViewCellWH * CGFloat(row) + HJPictureViewMargin * CGFloat(row - 1)
        
        return CGSize(width: width, height: height)
        
    }
    
    
    
    
    
    
    
    ///layout
    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(){
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        self.dataSource = self
        
        ///注册cell
        self.registerClass(HJStatusPictureViewCell.self, forCellWithReuseIdentifier: Cell)
        
        self.backgroundColor = UIColor.clearColor()
        
    }
    
    
    
}


extension HJStatusPictureView : UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picturesURL?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Cell, forIndexPath: indexPath) as! HJStatusPictureViewCell
        
        cell.imageUrl = picturesURL![indexPath.item]
        
        return cell
    }
    
}

///自定义Cell

class HJStatusPictureViewCell: UICollectionViewCell{
    
    
    //MARK: 属性
    ///图片的URL地址
    var imageUrl: NSURL?{
        
        didSet{
            
            if let url = imageUrl{
                
                bkgImageView.sd_setImageWithURL(url)
            }
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame:frame)
        
        prepareUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI(){
        
        ///添加子控件
        contentView.addSubview(bkgImageView)
        ///约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
        ///设置图片的填充模式
        
        bkgImageView.contentMode = UIViewContentMode.ScaleAspectFill
        ///超出imageView的剪掉
        
        bkgImageView.clipsToBounds = true
    }
    
    ///懒加载
    
    private lazy var bkgImageView: UIImageView = UIImageView()
    
    
    
    
}


